//
//  BaseroutViewController.m
//  GYMWinners
//
//  Created by Martin Ma on 12-06-16.
//  Copyright (c) 2012 AJB Software Design Inc. All rights reserved.
//

#import "BaseroutViewController.h"
#import "BaseroutAPI.h"

#import "MBProgressHUD.h"

@implementation BaseroutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem:newBackButton];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated{
    [self zStopHttpRequest];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void) zSendServer:(NSMutableDictionary *)slist theTitle:(NSString*) title theCommand:(NSString *)command{
    
    NSString *parmlist = @"";
    NSArray *listkeys = [slist allKeys];
    
    for (NSString *key in listkeys) {
        NSString *listvalue = [slist objectForKey:key];
        if( [parmlist isEqualToString:@""] )
        {
            parmlist = [NSString stringWithFormat:@"%@=%@", key, listvalue];
        }
        else
        {
            parmlist = [NSString stringWithFormat:@"%@&%@=%@", parmlist, key, listvalue];
        }
    }
    
    NSString *urlroot = [[BaseroutAPI sharedInstance] zGetVarEx:@"ServerIP"];
    NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@/mobile%@?%@", urlroot, command, parmlist] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSLog(@"Request=%@", url);   
    BaseroutHttpRequest = [ASIFormDataRequest requestWithURL:url];
    [BaseroutHttpRequest setRequestMethod:@"GET"]; 
    [BaseroutHttpRequest addRequestHeader:@"Content-type" value:@"application/json"]; 
    [BaseroutHttpRequest setDelegate:self];
    [BaseroutHttpRequest startAsynchronous];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = title;  
    
}


- (void) zStopHttpRequest{
    
    if( BaseroutHttpRequest != nil )
    {
        NSLog(@"call clearDelegatesAndCancel");
        [BaseroutHttpRequest clearDelegatesAndCancel];
    }
    else
    {
        //NSLog(@"do not call clearDelegatesAndCancel as it is nil");
    }
}

@end
